#include <jni.h>
#include <string>
#include <android/log.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "JNI---"

#ifdef __cplusplus
extern "C" {
#endif

const char *DUMP_WIFI_INFO = "/system/bin/dumpsys wifi";

JNIEXPORT jstring JNICALL
Java_com_example_sain_wifiapplication_CommonDrive_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

JNIEXPORT jstring JNICALL
Java_com_example_sain_wifiapplication_CommonDrive_getWifiMacAddress(
       JNIEnv* env,
       jobject /* this */) {

    char buf[1024] = {'\0'};
    FILE *fp;
    fp = popen(DUMP_WIFI_INFO, "r");  // 打开管道
    std::string error = "Failed to run command\n";
    if (fp == NULL) {
        __android_log_print(4, LOG_TAG, "Failed to run command\n");
        env->NewStringUTF(error.c_str());
    }

    while(fgets(buf, sizeof(buf), fp) != NULL) // 获取每行的返回信息
    {
        __android_log_print(4, LOG_TAG, "%s", buf);
    }

    pclose(fp);
    return env->NewStringUTF(buf);
}

#ifdef __cplusplus
}
#endif

